//#include <QDir>
#include "stdafx.h"
#include "scenarist.h"
#include "pythonscript.h"

Scenarist::Scenarist(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	//ui.centralWidget->setBackgroundColor(new QColor(58, 110, 165, 255));
	/*QTreeWidgetItem *item=new QTreeWidgetItem();
	item->setText(0, "new");
	QTreeWidgetItem *item2=new QTreeWidgetItem();
	item2->setText(0, "new");
	QTreeWidgetItem *item3=new QTreeWidgetItem();
	item3->setText(0, "new");
	ui.treeWidget->addTopLevelItem(item);
	item->addChild(item2);
	//ui.treeWidget->addTopLevelItem(item2);
	ui.treeWidget->addTopLevelItem(item3);*/
	//QMessageBox::aboutQt(this, "SIFScenarist");
	//connect(ui.textEdit, SIGNAL(update()), this, SLOT(onExit()));
	sett=new Settings(this);
	sett->setVisible(false);
}
// http://vk.com/album-33054304_148593181
Scenarist::~Scenarist()
{

}
void Scenarist::onExit(void)
{
	QMessageBox::aboutQt(this, "SIFScenarist");
}
void Scenarist::on_actionAboutQT_click(void)
	{QMessageBox::aboutQt(this, "SIFScenarist");}
void Scenarist::fun_not_implemented(void)
	{QMessageBox::about(this, "SIFScenarist", "Function not yet implemented!");}
void Scenarist::newFile(void)
	{
	QFileDialog* opFile=new QFileDialog();
	//opFile->
	QString Filename = 
		opFile->getSaveFileName( this,QString("Save file"), 
		QString(),QString("Synfig Files (*.scn);;All (*.*)"));
	if(Filename==""){QMessageBox::about(this, "SIFScenarist", "There is no files to save!");}
	else
		{

		
		////////////////////////////////////////////////
		bool result=false;
		QFile fileAccess;//(FileName + ", txt");
		fileAccess.setFileName(Filename);
		QString vers;
		if(fileAccess.open(QIODevice::WriteOnly))
			{
			QTextStream writer(&fileAccess);
			QString str="v.0.0.1\n";
			writer << str;
			writer << "Scene first_1\n";
			writer << "first_1.name=First\n";
			writer << "first_1.number=1\n";
			writer << "first_1.description=null\n";
			writer << "first_1.duration=5\n";
			fileAccess.close();
			}
		////////////////////////////////////////////////////
		//QString Filename="";
		scenes=new Scenario(Filename);//			creates new scenario

		//ui.centralWidget->setCaption(ui.centralWidget->caption() + " - " + Filename);
		QString title=Filename;
		//ui.centralWidget->setWindowTitle(title);
		QLabel* lab=new QLabel();
		if(scenes->isReady())
			{
			lab->setText(title);
			ui.statusBar->insertWidget(1, lab);
			ui.centralWidget->update();
			ui.actionOpen->setEnabled(false);
			ui.actionNew->setEnabled(false);
			this->drawTree();
			//																menu cleaning
			}
		else{fun_not_implemented();}
		}
	}
void Scenarist::openFile(void)
{
	QFileDialog* opFile=new QFileDialog();
	//opFile->
	QString Filename = opFile->getOpenFileName( this,QString("Open file"), QString(),QString("Synfig Files (*.scn);;All (*.*)"));
	if(Filename==""){QMessageBox::about(this, "SIFScenarist", "There is no files to open!");}
	else
	{
		scenes=new Scenario(Filename);//			creates new scenario

		//ui.centralWidget->setCaption(ui.centralWidget->caption() + " - " + Filename);
		QString title=Filename;
		//ui.centralWidget->setWindowTitle(title);
		QLabel* lab=new QLabel();
		
		if(scenes->isReady())
		{
			lab->setText(title);
			ui.statusBar->insertWidget(1, lab);
			ui.centralWidget->update();
			ui.actionOpen->setEnabled(false);
			ui.actionNew->setEnabled(false);
			this->drawTree();
		//																menu cleaning
		}
	}
}
//
void Scenarist::openSettings(void)
	{
	Qt::WindowFlags flag=Qt::Tool;
	
	sett->setWindowFlags(flag);
	//sett->showNormal();
	sett->show();
	}

void Scenarist::drawTree()
	{
	bool result=false;
	QTreeWidgetItem *item=new QTreeWidgetItem();
	if(scenes->isReady()){
		int kolvo=scenes->GetQuantityOfScenes();
		int i = kolvo-1;
		//MessageBox::Show(kolvo.ToString());
		Scene * sc;
		//TreeNode^ upper=treeView1->TopNode;
		item->setText(0,scenes->GetProjectName());
		ui.treeWidget->addTopLevelItem(item);
		//upper=upper->FirstNode;
		try{
		while(i>-1)
			{
			QTreeWidgetItem * child_=new QTreeWidgetItem();
			sc = scenes->GetNextScene(i);
			child_->setText(0,sc->GetOuterName());
			if(scenes->isReady())item->insertChild(0,child_); //upper->Nodes->Add();
			i--
				;
			}
		}
		catch(int ik){}
		}
	ui.treeWidget->update();
	/////////////////////////////////////////////////////////////////////////
	/*
	QTreeWidgetItem *item=new QTreeWidgetItem();
	item->setText(0, "new");
	QTreeWidgetItem *item2=new QTreeWidgetItem();
	item2->setText(0, "new");
	QTreeWidgetItem *item3=new QTreeWidgetItem();
	item3->setText(0, "new");
	ui.treeWidget->addTopLevelItem(item);
	item->addChild(item2);
	//ui.treeWidget->addTopLevelItem(item2);
	ui.treeWidget->addTopLevelItem(item3);
	*/
	/////////////////////////////////////////////////////////////////////////
	//scenes->Save();
	return;// result;
	}

void Scenarist::on_actionAbout(void)
	{QMessageBox::about(this, tr("About SIFScenarist"),
	tr("The <b>SIFScenarist (v. 0.0.1.4 pre-alpha)</b> is written by A. Metellius"));
	QProcess* proc=new QProcess(this);
	proc->start("notepad");
	}
void Scenarist::treeDobleClicked()
	{
		QMessageBox::about(this, "SIFScenarist","tree double clicked");
	}
void Scenarist::treeClicked(QTreeWidgetItem* item, int)
	{
	
	int kolvo=scenes->GetQuantityOfScenes();
	int i = kolvo-1;
	//MessageBox::Show(kolvo.ToString());
	Scene * sc;
	//TreeNode^ upper=treeView1->TopNode;
	//item->text(0,scenes->GetProjectName());
	//ui.treeWidget->addTopLevelItem(item);
	//upper=upper->FirstNode;
	try{
		while(i>-1)
			{
			/*QTreeWidgetItem * child_=new QTreeWidgetItem();
			sc = scenes->GetNextScene(i);
			child_->setText(0,sc->GetOuterName());
			if(scenes->isReady())item->insertChild(0,child_); //upper->Nodes->Add();
			i--*/
			sc = scenes->GetNextScene(i);
			if(item->text(0)==sc->GetOuterName())
				{
				QMessageBox::about(this, "SIFScenarist",item->text(0) + " clicked; description: "
					+ "not yet implemented");//sc->GetDescription());
				i=-1;
				}
			i--;
			}
		}
		
	catch(int ik){}
	}
void Scenarist::python(void)
	{
	QFileDialog* opFile=new QFileDialog();
	//opFile->
	/*
	QString getOpenFileName
	(
		QWidget * parent = 0, 
		const QString & caption = QString(), 
		const QString & dir = QString(),
		const QString & filter = QString(),
		QString * selectedFilter = 0,
		Options options = 0
	)*/
	QString Filename = opFile->getOpenFileName( this,
		QString("Open python file"), 
		QString(QDir::currentPath()),
		QString("Python Script (*.py);;All (*.*)"));
	if(Filename==""){QMessageBox::about(this, "SIFScenarist", "There is no files to open!");}
	else
		{
	
		//ui.centralWidget->setCaption(ui.centralWidget->caption() + " - " + Filename);
		QString title=Filename;
		//ui.centralWidget->setWindowTitle(title);
		QLabel* lab=new QLabel();

		
		lab->setText(title);
		ui.statusBar->insertWidget(1, lab);
		ui.centralWidget->update();
		PythonScript* script;
		script=new PythonScript(title);
		if(!script->valid)
			{
			QMessageBox::about(this, 
				"SIFScenarist", 
				"Function not yet implemented!");}
			//																menu cleaning
			
		}
		
	}